
//邮箱以及手机的正则表达式
function regexConfig() {
  var reg = {
    email: /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/,
    phone: /^1(3|4|5|7|8)\d{9}$/,
    IDcard: /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/,
    bankCard: /^(\d{16}|\d{18}|\d{19})$/,
    price: /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/
  }
  return reg;
}

function location(that) {
  // 添加位置坐标
  wx.getLocation({
    type: 'gcj02', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
    success: function (res) {
      that.setData({
        location:{
          longitude: res.longitude,
        latitude: res.latitude
        }
      });
    },
  })
}

function chooseImg(that) {
  wx.chooseImage({
    count: 9, // 最多可以选择的图片张数，默认9
    sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
    sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
    success: function (res) {
      // success
      wx.uploadFile({
        url: 'http://1.aiym.applinzi.com/test.php',
        filePath: res.tempFilePaths[0],
        name: 'file',
        formData: {
          'user': 'test'
        },
        header: { "Content-Type": "multipart/form-data" },
        success: function (res) {
          that.setData({
            imgUrl: res.data
          })
        }
      })
    }
  })
}

function checkPassword(param, that) {
  var password = param.trim();
  // console.log(username, password)
  if (password.length <= 0) {
    wx.showModal({
      title: '提示',
      showCancel: false,
      content: '请设置密码'
    });
    return false;
  } else if (password.length < 6 || password.length > 20) {
    wx.showModal({
      title: '提示',
      showCancel: false,
      content: '密码长度为6-20位字符'
    });
    return false;
  } else {
    that.setData({
      password: password
    })
    return true;
  }
}

function checkPhoneNum(param, that) {
  var phone = regexConfig().phone;
  var inputPhoneNum = param.trim();
  if (phone.test(inputPhoneNum)) {
    return true;
  } else {
    wx.showModal({
      title: '提示',
      showCancel: false,
      content: '请输入正确的手机号码'
    });
    return false;
  }
}

function getSystemInfo(that) {
  wx.getSystemInfo({
    success: function (res) {
      that.setData({
        screen: {
          height: res.windowHeight + 'px',
          winth: res.windowWidth + 'px'
        }
      })
    }
  })
}

 function checkId (param) {
    var IDcard = regexConfig().IDcard;
    if (IDcard.test(param)) {
      return true;
    } else {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请输入正确格式的身份证号码'
      });
      return false;
    }
  }

  function checkBankcard(param) {
    var bankCard = regexConfig().bankCard;
    if (bankCard.test(param)) {
      return true;
    } else {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请输入正确格式的银行卡号'
      });
      return false;
    }
  }


function pushMess(shopId, that){
  wx.request({
    url: 'http://1.aiym.applinzi.com/aiyouwai.php',
    data: {
      type:"judgeOrder",
      paras:{
        uid:shopId
      }
    },
    method: 'GET', 
    header: {"Content-Type":"application/json"}, // 设置请求的 header
    success: function(res){
      // success
      console.log(res.data)
      if(res.data){
        that.setData({
          hasOrder:true,
          orderList:res.data
        })
      }
    }
  })
}
function judgePrice(num){
  var price = regexConfig().price
  if(price.test(num)){
    return true;
  }else{
    wx.showModal({
      content:"请输入正确的价格格式"
    })
  }

}
module.exports = {
  regexConfig: regexConfig,
  chooseImg: chooseImg,
  checkPassword: checkPassword,
  checkPhoneNum: checkPhoneNum,
  location: location,
  checkId:checkId,
  checkBankcard:checkBankcard,
  pushMess:pushMess,
  judgePrice,judgePrice
}
